package com.teach.commons;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;

/**
 * <pre>
 * TODO : 密码工具类
 * ClassName : com.teach.commons.PasswordUtil
 * Author : J.L.Zhou
 * Date : 2020-06-30 10:24
 * Version : V1.0
 * Copyright 2020 jlzhou.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 */
public class PasswordUtil {

	private static Digester md5 = new Digester(DigestAlgorithm.MD5);

	private PasswordUtil() {
	}

	/**
	 * - 含盐值的加密
	 * 
	 * @param password 密码明文
	 * @param salt     盐值
	 * @return
	 */
	public static String encode(String password, String salt) {
		if (StrUtil.hasEmpty(password)) {
			throw new RuntimeException("密码不能为空");
		}
		if (StrUtil.hasEmpty(salt)) {
			throw new RuntimeException("盐值不能为空");
		}
		return md5.digestHex(password + salt);
	}
	

	/**
	 * - 含盐值的多次加密
	 * 
	 * @param password 密码明文
	 * @param salt     盐值
	 * @param num      加密次数
	 * @return
	 */
	public static String encode(String password, String salt, int num) {
		if(num<1) {
			throw new RuntimeException("加密次数不能小于1");
		}
		String ret = encode(password, salt);
		if (num == 1) {
			return ret;
		} else {
			return encode(ret, salt, num - 1);
		}
	}
}
